package com.ruoyi.workorder.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.workorder.domain.WorkordersCatalog;
import com.ruoyi.workorder.service.IWorkordersCatalogService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工单类别Controller
 * 
 * @author songxin
 * @date 2024-07-09
 */
@RestController
@RequestMapping("/workorder/workorderType")
public class WorkordersCatalogController extends BaseController
{
    @Autowired
    private IWorkordersCatalogService workordersCatalogService;

    /**
     * 查询工单类别列表
     */
    @PreAuthorize("@ss.hasPermi('workorder:workorderType:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkordersCatalog workordersCatalog)
    {
        startPage();
        List<WorkordersCatalog> list = workordersCatalogService.selectWorkordersCatalogList(workordersCatalog);
        System.out.println("list =这是查询工单类别的 " + list);
        return getDataTable(list);
    }

    /**
     * 导出工单类别列表
     */
    @PreAuthorize("@ss.hasPermi('workorder:workorderType:export')")
    @Log(title = "工单类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkordersCatalog workordersCatalog)
    {
        List<WorkordersCatalog> list = workordersCatalogService.selectWorkordersCatalogList(workordersCatalog);
        ExcelUtil<WorkordersCatalog> util = new ExcelUtil<WorkordersCatalog>(WorkordersCatalog.class);
        util.exportExcel(response, list, "工单类别数据");
    }

    /**
     * 获取工单类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('workorder:workorderType:query')")
    @GetMapping(value = "/{woCatalogId}")
    public AjaxResult getInfo(@PathVariable("woCatalogId") Long woCatalogId)
    {
        return success(workordersCatalogService.selectWorkordersCatalogByWoCatalogId(woCatalogId));
    }

    /**
     * 新增工单类别
     */
    @PreAuthorize("@ss.hasPermi('workorder:workorderType:add')")
    @Log(title = "工单类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated WorkordersCatalog workordersCatalog)
    {
        Long id = workordersCatalogService.getWorkordersCatalogName(workordersCatalog);
        if(id != null){
            return error("工单类别名称已存在");
        }
        return toAjax(workordersCatalogService.insertWorkordersCatalog(workordersCatalog));
    }

    /**
     * 修改工单类别
     */
    @PreAuthorize("@ss.hasPermi('workorder:workorderType:edit')")
    @Log(title = "工单类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkordersCatalog workordersCatalog)
    {
        Long id = workordersCatalogService.getWorkordersCatalogName(workordersCatalog);
        if(id != null){
            return error("工单类别名称已存在");
        }
        return toAjax(workordersCatalogService.updateWorkordersCatalog(workordersCatalog));
    }

    /**
     * 删除工单类别
     */
    @PreAuthorize("@ss.hasPermi('workorder:workorderType:remove')")
    @Log(title = "工单类别", businessType = BusinessType.DELETE)
	@DeleteMapping("/{woCatalogIds}")
    public AjaxResult remove(@PathVariable Long[] woCatalogIds)
    {
        List<String> result = workordersCatalogService.getWorkordersCatalogNameConstraint(woCatalogIds);
        if(result != null && !result.isEmpty()){
            String gongdanNames = result.stream().map(String::valueOf).collect(Collectors.joining(","));
            return AjaxResult.error("删除失败, 工单类别 " + gongdanNames + " 被工单使用，不能删除！");
        }
        return toAjax(workordersCatalogService.deleteWorkordersCatalogByWoCatalogIds(woCatalogIds));
    }
}
